package com.youchuang.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.youchuang.bean.Comment;
import com.youchuang.common.HibernateConnection;
import com.youchuang.dao.CommentDAO;

@Repository("CommentDAO")
public class CommentDAOImpl implements CommentDAO {

	private static final String HQL_FIND_ALL = "FROM Comment";
	private static final String HQL_FIND_ALL_COUNT = "SELECT COUNT(c.id) FROM Comment c";
	private static final String HQL_FIND_BY_PRODUCTID = "FROM Comment WHERE productId = :productId";

	@Resource
	private HibernateConnection<Comment> hibernateConnection;

	@Override
	public void save(Comment t) {
		hibernateConnection.save(t);
	}

	@Override
	public void delete(Comment t) {
		//Do nothing because of it unusual
	}

	@Override
	public int update(Comment t) {
		//Do nothing because of it unusual
		return 0;
	}

	@Override
	public List<Comment> queryAll() {
		return hibernateConnection.find(HQL_FIND_ALL);
	}

	@Override
	public List<Comment> queryByProductId(String productId) {

		Map<String, Object> params = new HashMap<>();
		params.put("productId", productId);
		return hibernateConnection.find(HQL_FIND_BY_PRODUCTID, params);
	}

	@Override
	public List<Comment> queryLimit(int first, int max) {
		return hibernateConnection.find(HQL_FIND_ALL, first, max);
	}

	@Override
	public int queryAllCount() {
		return hibernateConnection.count(HQL_FIND_ALL_COUNT).intValue();
	}

}
